package com.example.doctorsystem.controller;

import com.example.doctorsystem.model.Medicine;
import com.example.doctorsystem.service.MedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/medicines")
@CrossOrigin(origins = "*")
public class MedicineController {

    @Autowired
    private MedicineService medicineService;

    // 添加药品
    @PostMapping
    public String add(@RequestBody Medicine medicine) {
        return medicineService.addMedicine(medicine) > 0 ? "添加成功" : "添加失败";
    }

    // 查询所有药品
    @GetMapping
    public List<Medicine> list() {
        return medicineService.getAllMedicines();
    }

    // 根据 ID 查询药品
    @GetMapping("/{id}")
    public Medicine getById(@PathVariable int id) {
        return medicineService.getMedicineById(id);
    }

    // 更新药品
    @PutMapping
    public String update(@RequestBody Medicine medicine) {
        return medicineService.updateMedicine(medicine) > 0 ? "更新成功" : "更新失败";
    }

    // 删除药品
    @DeleteMapping("/{id}")
    public String delete(@PathVariable int id) {
        return medicineService.deleteMedicineById(id) > 0 ? "删除成功" : "删除失败";
    }
}
